﻿using System;
using System.Data;
using System.Data.SqlClient;
using Coogle.Core;

namespace Coogle.DataAccess
{
    public class SelectContainer : IFindOneQuery<Container>
    {
        private const String QUERY = @"SELECT Sign, Number, Checkdigit, Type FROM Containers WHERE Sign=@Sign AND Number=@Number AND Checkdigit=@Checkdigit";

        private readonly ContainerCode m_containerCode;

        public SelectContainer(ContainerCode containerCode)
        {
            this.m_containerCode = containerCode;
        }

        public SqlCommand CreateCommand(SqlConnection connection)
        {
            var cmd = new SqlCommand(QUERY, connection);
            cmd.Parameters.AddWithValue("Sign", m_containerCode.Sign);
            cmd.Parameters.AddWithValue("Number", m_containerCode.Number);
            cmd.Parameters.AddWithValue("Checkdigit", m_containerCode.Check);
            return cmd;
        }

        public Container Map(DataRow row)
        {
            if (row == null)
                return null;

            return new Container(row["Sign"].ToString(),
                                 Int32.Parse(row["Number"].ToString()),
                                 row["Checkdigit"].ToString(),
                                 row["Type"].ToString());
        }
    }
}
